﻿/* 10. Write a program that finds in given array of integers a sequence of given sum S (if present). 
 * Example:	{4, 3, 1, 4, 2, 5, 8}, S = 11 -> {4, 2, 5} */

using System;

public class SequenceOfGivenSum
{
    public static void Main()
    {
        int n;
        double sum;
        double currentSum;
        bool thereIsNot = true;

        Console.Write("\nThe length of array: ");
        n = int.Parse(Console.ReadLine());

        if (0 < n)
        {
            double[] array = new double[n]; // the program will works and with double members of array
            Console.WriteLine();
            for (int i = 0; i < n; i++)
            {
                Console.Write("Array[{0}] = ", i + 1);
                array[i] = double.Parse(Console.ReadLine());
            }

            Console.Write("\nand the sum S = ");
            sum = double.Parse(Console.ReadLine());

            for (int i = 0; i < n; i++)
            {
                if (!thereIsNot)
                    break;

                if (array[i] == sum)
                {
                    thereIsNot = false;
                    Console.WriteLine("\nThe first sequence of given sum {0} is {1} {0} {2}\n", sum, "{", "}");
                    break;
                }
                else
                {
                    currentSum = array[i];
                    for (int j = i + 1; j < n; j++)
                    {
                        currentSum += array[j];
                        if (currentSum == sum)
                        {
                            thereIsNot = false;
                            Console.Write("\nThe first sequence of given sum {0} is\n {1} {2}", sum, "{", array[i]);
                            for (int k = i + 1; k <= j; k++)
                                Console.Write(", {0}", array[k]);

                            Console.WriteLine(" {0}\n", "}");
                            break;
                        }
                    }
                }
            }

            if (thereIsNot)
                Console.WriteLine("\nIn array there isn't sequence with sum {0}.\n", sum);
        }
        else
            Console.WriteLine("\nThe length of array must be natural!\n");
    }
}